<?php
// +----------------------------------------------------------------------
// | 产品修改附属操作，例如：美团推送
// +----------------------------------------------------------------------
// | TongBaoChun <501468940@qq.com>
// +----------------------------------------------------------------------
// | 2015/09/18
// +----------------------------------------------------------------------
include '/var/www/html/new/com.inc.php';
if ($_REQUEST['auth'] == 'admin') {
    $_SESSION = $_REQUEST['session'];
}
if(!isset($_SESSION['sid'])) exit('{"status":"fail","msg":"登陆超时","code":0}');



$pid = $_POST['pid']+0;
$did = $_POST['did']+0;
$sid = $_SESSION['sid'];

// 去除购买权限
$sql="select pids from `pft_product_sale_list` where aid=$sid and fid=$did limit 1";
$GLOBALS['le']->query($sql);
$GLOBALS['le']->fetch_assoc();
$pids_db = $GLOBALS['le']->f('pids');
if($pids_db=='A'){
    $sql = "select id from uu_products where apply_did=$sid and p_status<6 and apply_limit<6";
	$GLOBALS['le']->query($sql);
	while($row=$GLOBALS['le']->fetch_assoc()) $pids_a[] = $row['id'];
}else $pids_a = explode(',', $pids_db);

$pids_s = '';
$pids_a = array_diff($pids_a, array($pid));
if(count($pids_a)) $pids_s = implode(',', $pids_a);
$sql = "update `pft_product_sale_list` set pids='$pids_s' where aid=$sid and fid=$did limit 1";
if(!$GLOBALS['le']->query($sql)) exit('{"status":"fail","msg":"清除权限失败"}');
// echo $sql;
// 去除底下的所有购买权限
$evolist = RecvEvoluteRow($pid, $did, "$sid,$did");
if(count($evolist)) foreach($evolist as $row) $idBox_a[] = $row['id'];
$idBox_l = count($idBox_a);
if($idBox_l==0) exit('{"status":"success","msg":"设置成功"}');
	
$idBox_s = implode(',', $idBox_a);
$sql = "update pft_p_apply_evolute set status=1,active=0 where id in ($idBox_s) and pid=$pid limit $idBox_l";
$GLOBALS['le']->query($sql);
exit('{"status":"success","msg":"设置成功"}');
// print_r($evolist);






//  递归获取树形产品供应链
function RecvEvoluteRow($pid, $sid, $aids=''){
	
	$container = array();
	$sql = "select * from pft_p_apply_evolute where pid=$pid and sid=$sid and status=0";
	if($aids!='') $sql = $sql." and aids='$aids'";
	$GLOBALS['le']->query($sql);
	while($row=$GLOBALS['le']->fetch_assoc()) $container[] = $row;
	if(count($container)){
		foreach($container as $row){
			$container = array_merge($container, RecvEvoluteRow($pid, $row['fid'], $row['aids'].','.$row['fid']));
		}
	}
	return $container;
}





exit;

/* 
$did=$_REQUEST['did']; // 分销商 ID
$sid=$_SESSION['sid']; // 会员 ID
$set_pris=$_REQUEST["set_pris"]; // 配置价格
$src_pris=$_REQUEST["src_pris"]; // 原来价格

$pids_e = $_REQUEST['exi_pids']; // 页面显示的产品 id
$pids_s = $_REQUEST["set_pids"]; // 设置的产品 id
$others = $_REQUEST['others_f']; // 非自供产品标志，0 为自己供应 1 为分销

$pids_f = array();// 分销产品

// 先去除页面显示的 Pid，再添加有配置的 Pid
$sql="select pids from `pft_product_sale_list` where aid=$sid and fid=$did limit 1";
$GLOBALS['le']->query($sql);
$GLOBALS['le']->fetch_assoc();
$pids_db = $GLOBALS['le']->f('pids');
if($pids_db=='A'){
    $pids_db = '';
}else{
    $pids_db_tmp = explode(',', $pids_db);
    $pids_db_tmp = array_diff($pids_db_tmp,$pids_e);// 去除页面显示的产品
}

$pids_other = array();
foreach($pids_s as $pid){
    if($others[$pid]){// 分销产品
        $pids_f[$pid] = $pid;
        continue;
    }
    $pids_db_tmp[]= $pid;
}

// 更新分销商可购买的产品 ID
$pids_db = implode(',', $pids_db_tmp);
$sql="update `pft_product_sale_list` set pids='$pids_db' where aid=$sid and fid=$did limit 1";
// echo $sql;
$GLOBALS['le']->query($sql);

// 打勾的转分销产品设置
foreach($pids_f as $key=>$pid){
    // 是否存在
    $sql="select id,status from `pft_p_apply_evolute` where pid=$pid and fid=$did and sid=$sid limit 1";
    $GLOBALS['le']->query($sql);
    if($GLOBALS['le']->fetch_assoc()){
        if($GLOBALS['le']->f('status')==0){
            $sql="update `pft_p_apply_evolute` set status=0 where pid=$pid and fid=$did and sid=$sid limit 1";
            $GLOBALS['le']->query($sql);
            // echo $sql;
        }
        continue;
    }
    
    // 不存在获取分销商信息
    $sql="select id,lvl,aids,sourceid from `pft_p_apply_evolute` where pid=$pid and fid=$sid limit 1";
    $GLOBALS['le']->query($sql);
    if(!$GLOBALS['le']->fetch_assoc()) continue;
    $sourceid = $GLOBALS['le']->f('sourceid');
    $aids = $GLOBALS['le']->f('aids').','.$sid;
    $lvl = $GLOBALS['le']->f('lvl') + 1;
    $ins="insert into `pft_p_apply_evolute` set pid=$pid,fid=$did,sid=$sid,sourceid=$sourceid,aids='$aids',lvl=$lvl,rectime=now()";
    echo $ins;
}

// 去除未打勾的转分销购买权限
foreach($pids_e as $pid){
    // 非转分销产品或者是要设置的
    if($others[$pid]<1 || isset($pids_f[$pid])) continue;
    $sql="update `pft_p_apply_evolute` set status=1 where pid=$pid and fid=$did and sid=$sid limit 1";
    $GLOBALS['le']->query($sql);
    // echo $sql;
}


// 价格设置
$exis = $exis_u = $exis_uid = array();
$sql="select pid,id,tid from `uu_priceset` where pid=$did AND tid in (".implode(',',$pids_s).")";
$GLOBALS['le']->query($sql);
while($row=$GLOBALS['le']->fetch_assoc()){
    $exis[$row['tid']]     = $row['tid'];// 获取已配置价格的产品 id 
    $exis_uid[]            = $row['id'];
}

foreach($set_pris as $pid=>$pri){
    if($pri<$src_pris[$pid]) continue;// 设置的新价格比原价格小
    $diff = ($pri - $src_pris[$pid])*100;
    $aid = ($others[$pid]>0) ? $sid:0;
    // 是否存在记录
    if(isset($exis[$pid])){
        $sql="update `uu_priceset` set dprice=$diff where tid=$pid and pid=$did and aid=$aid limit 1";
    }else{
        $sql="insert into `uu_priceset` set dprice=$diff,tid=$pid,pid=$did,aid=$aid";
    }
    // echo $sql;
    $GLOBALS['le']->query($sql);
}

echo "<script>alert('配置成功!')</script>";
exit; */
// print_r($_REQUEST);
// exit;

?>